﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CommandLine;
using CommandLine.Text;
using MakeTableTool;
using MakeTableTool.Template;

namespace MakeTableConsoleWin
{
    class Options
    {
        [Option('s', "src", Required = true, HelpText = "输入数据文件")]
        public string Directory { get; set; }

        [Option('d', "data", Required = true, HelpText = "输出数据路径")]
        public string OutputPath { get; set; }

        [Option('c', "codeFile", Required = false, HelpText = "输出代码路径")]
        public string OutputCodeFilePath { get; set; }

        [Option('t', "teamplateFile", Required = false, HelpText = "模板代码路径")]
        public string TeamplateFilePath { get; set; }

        [Option("jsonPath",Required = false,HelpText = "增量打表对比文件")]
        public string IncrementalJsonPath { get; set; }
        [ParserState]
        public IParserState LastParserState { get; set; }

        [HelpOption]
        public string GetUsage()
        {
            return HelpText.AutoBuild(this,
                (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
        }

    }
    class Program
    {
        static void Main(string[] args)
        {
            var options = new Options();
            if (CommandLine.Parser.Default.ParseArguments(args, options))
            {
                try
                {
                    Console.WriteLine("打表 : " + options.Directory);
                    DirectoryInfo directoryInfo = new DirectoryInfo(options.Directory);
                    FileInfo[] fileInfos = directoryInfo.GetFiles("*.xlsx", SearchOption.TopDirectoryOnly);
                    if (options.TeamplateFilePath != null)
                    {
                        TemplateGenTool.InitTemplateFile(options.TeamplateFilePath);

                        if (TemplateGenTool.isInitTemplateFile == false)
                        {
                            Console.WriteLine("模板代码文件没有初始化");
                        }
                    }

                    //判断是否增量打包
                    bool isIncrementalMakeTable = false; //是否增量打包
                    List<FileInfo> dirtyfilePaths=null;
                    MD5Checker md5Checker = new MD5Checker();
                    if (options.IncrementalJsonPath != null)
                    {
                        
                        md5Checker.LoadMD5Info(options.IncrementalJsonPath);
                        dirtyfilePaths = md5Checker.GetDirtyFiles(fileInfos.ToList());

                        isIncrementalMakeTable = true;
                    }


                    if(isIncrementalMakeTable)
                    {
                        foreach (var fileInfo in dirtyfilePaths)
                        {
                            string filePath = fileInfo.FullName;
                            Excute(options, filePath);
                        }
                        //更新MD5文件
                        md5Checker.UpdateNewMD5Json(fileInfos.ToList(), options.IncrementalJsonPath);
                    }
                    else
                    {
                        foreach (var fileInfo in fileInfos)
                        {
                            string filePath = fileInfo.FullName;
                            Excute(options, filePath);
                        }
                    }
                    
                   
                }
                catch(Exception e)
                {
                    Console.WriteLine("打表终止！！！Error : {0},\n Stack : {1}", e.Message, e.StackTrace);
                }
            }

            Console.WriteLine("打表结束！！");
            Console.Read();
        }

       static void Excute(Options options ,string filePath)
        {
            var excelTable = MakeTable.LoadTable(filePath);
            if (excelTable != null)
            {
                if (!Directory.Exists(options.OutputPath))
                {
                    Directory.CreateDirectory(options.OutputPath);
                }
                //保存数据文件
                MakeTable.SaveFile(excelTable, options.OutputPath + "/" + excelTable.fileName + ".bytes");

                if (!string.IsNullOrEmpty(options.OutputCodeFilePath))
                {

                    if (!Directory.Exists(options.OutputCodeFilePath))
                    {
                        Directory.CreateDirectory(options.OutputCodeFilePath);
                    }
                    //保存代码文件
                    TemplateGenTool.GenTemplateFile(excelTable, options.OutputCodeFilePath + "/" + excelTable.fileName + ".cs");

                }
            }
        }

    }
}
